package com.talent.jiwen_teacher.ui.widgets;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.http.result.OrderListResult;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import static com.talent.jiwen_teacher.util.Constant.gradeMap;

public class CourseInfoView extends LinearLayout {
    private ImageView headIv;
    private TextView nameTv;
    private RecyclerView memberRecyclerView;
    private TextView contentTv;
    private RecyclerView imgRecyclerView;
    private ImageView txt_test;
    private TextView gradeTv;

    public CourseInfoView(Context context) {
        super(context);
        init();
    }

    public CourseInfoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CourseInfoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        inflate(getContext() , R.layout.view_course_info , this);

        headIv = (ImageView)findViewById(R.id.headIv);
        nameTv = (TextView)findViewById(R.id.nameTv);
        memberRecyclerView = (RecyclerView)findViewById(R.id.memberRecyclerView);
        contentTv = (TextView)findViewById(R.id.contentTv);
        imgRecyclerView = (RecyclerView)findViewById(R.id.imgRecyclerView);
        txt_test=findViewById(R.id.txt_test);
        gradeTv=findViewById(R.id.gradeTv);
    }

    public void bindData(OrderListResult.Order data){

        if (null != data){
            ImageUtil.loadCircleImage(getContext() , data.getAskStudentHeadImage() , headIv);

            nameTv.setText(data.getAskStudentName());
            contentTv.setText(data.getQuestionIntroduction());

            if(data.getGradeLevel()>0){
                gradeTv.setText(gradeMap.get(data.getGradeLevel()));
            }

            List<OrderListResult.Member> memberList = new ArrayList<>();
            if (!Validators.isEmpty(data.getMemberList())){
                memberList.clear();
                memberList.addAll(data.getMemberList());
            }
            LinearLayoutManager memberLayoutManager = new LinearLayoutManager(getContext());
            memberLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            memberRecyclerView.setLayoutManager(memberLayoutManager);
            memberRecyclerView.setAdapter(new CommonAdapter<OrderListResult.Member>(getContext() , R.layout.item_course_member , memberList){
                @Override
                protected void convert(ViewHolder holder, OrderListResult.Member member, int position) {
                    ImageView memberIv = holder.getView(R.id.memberIv);
                    ImageUtil.loadCircleImage(getContext() , member.getStudentHeadImage() , memberIv);
                }
            });
            memberRecyclerView.setVisibility(GONE);

            if(data.getOrderType()==1){
                txt_test.setVisibility(VISIBLE);
            }else{
                txt_test.setVisibility(GONE);
            }


            if (!Validators.isEmpty(data.getQuestionImages())){
                imgRecyclerView.setVisibility(VISIBLE);
                final List<String> imgList = new ArrayList<>();
                String[] imgs = data.getQuestionImages().split(",");
                for (int i = 0 ; i < imgs.length ; i++){
                    imgList.add(imgs[i]);
                }
                LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
                imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                imgRecyclerView.setLayoutManager(imgLayoutManager);
                imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img , imgList){
                    @Override
                    protected void convert(ViewHolder holder, String s, final int position) {
                        ImageView imgIv = holder.getView(R.id.imgIv);
                        ImageUtil.loadCorner4Image(getContext() , s , imgIv);
                        imgIv.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , imgList);

                            }
                        });
                    }
                });
            }else{
                imgRecyclerView.setVisibility(GONE);
            }

        }else{
            ImageUtil.loadCircleImage(getContext() , "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg" , headIv);

            nameTv.setText("黄雨菲");
            contentTv.setText("dasdaskhdjashdjashdjkashdkjashdkjsahkjdhasjdhaskjd");

            List<String> memberList = new ArrayList<>();
            for (int i = 0 ; i < 5 ; i ++){
                memberList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg");
            }
            LinearLayoutManager memberLayoutManager = new LinearLayoutManager(getContext());
            memberLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            memberRecyclerView.setLayoutManager(memberLayoutManager);
            memberRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_course_member , memberList){
                @Override
                protected void convert(ViewHolder holder, String s, int position) {
                    ImageView memberIv = holder.getView(R.id.memberIv);
                    ImageUtil.loadCircleImage(getContext() , s , memberIv);
                }
            });


            final List<String> imgList = new ArrayList<>();
            for (int i = 0 ; i < 3 ; i ++){
                imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg");
            }
            LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
            imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            imgRecyclerView.setLayoutManager(imgLayoutManager);
            imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img , imgList){
                @Override
                protected void convert(ViewHolder holder, String s, final int position) {
                    ImageView imgIv = holder.getView(R.id.imgIv);
                    ImageUtil.loadCorner4Image(getContext() , s , imgIv);
                    imgIv.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , imgList);

                        }
                    });
                }
            });
        }
    }
}
